_realname=nvc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
repo_root_dir=$(git rev-parse --show-toplevel)
_pkgver() {
  (cd "${repo_root_dir}";\
   vers=$(sed -n '1s/AC_INIT(\[.*\], \[\([0-9\.]*\).*\].*/\1/p' configure.ac);\
   git log --date=format:%Y%m%d --pretty=$vers~git%cd.%h -1)
}
pkgver="$(_pkgver)"
pkgrel=1
pkgdesc="VHDL compiler and simulator (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url='https://www.nickg.me.uk/nvc/'
license=('GPL3')
groups=("${MINGW_PACKAGE_PREFIX}-eda")
depends=(
  "${MINGW_PACKAGE_PREFIX}-llvm" # required by NVC for compiling
  "${MINGW_PACKAGE_PREFIX}-cc"   # required by NVC for linking
)
makedepends=(
  "base-devel"
  "git"
  "autoconf"
  "automake-wrapper"
  "${MINGW_PACKAGE_PREFIX}-ncurses"
  "${MINGW_PACKAGE_PREFIX}-libffi"
  "${MINGW_PACKAGE_PREFIX}-check"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
)

pkgver() {
  _pkgver
}

prepare() {
  mkdir -p "${srcdir}/${_realname}-${pkgver}"
  cd "${repo_root_dir}"

  ./autogen.sh
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  "${repo_root_dir}"/configure \
    --prefix="${MINGW_PREFIX}" \
    --build="${MINGW_CHOST}" \
    --host="${MINGW_CHOST}" \
    --target="${MINGW_CHOST}" \
    --enable-static \
    --enable-shared \
    --program-prefix= \
    --with-system-cc=cc

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  make check
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  make install DESTDIR="${pkgdir}"
}
